home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / What's On My Mac / GrabFrontWindow.c < prev    next >
Text File  |  1995-06-24  |  5KB  |  214 lines

  1. /*****
  2.  *
  3.  *    What's On My Mac
  4.  *
  5.  *    By Scott T Boyd (with help from Grant Neufeld & Jorg... )
  6.  *
  7.  *    GrabFrontWindow.c
  8.  *
  9.  *    Copyright ©1995 by Scott T Boyd
  10.  *
  11.  *    This source may be freely used as long as the copyright notice is kept in the source.
  12.  *    I (we) ask that you let us know of any enhancements (read: bug fixes) to this code.
  13.  *    I (we) would also like copies of (or discounts on) anything you produce this with, please.
  14.  *
  15.  *    http://www.hax.com/
  16.  *    scott@hax.com
  17.  *
  18.  *****/
  19.  
  20. #include <string.h>
  21. #include "Layers.h"
  22. #include "ImageCompression.h"
  23. #include "Sound.h"
  24. #include "GrabFrontWindow.h"
  25.  
  26.  
  27. //Play the camera click
  28. void
  29. SnapshotSound ( void )
  30. {
  31.     short            err;
  32.     SndChannelPtr    chan;
  33.     SndListHandle    sndHdl;
  34.     Boolean            async;
  35.     
  36.     chan    = NULL;
  37.     sndHdl    = (SndListHandle) GetResource ( 'snd ', 0xbf88 ); 
  38.     async    = false;
  39.     
  40.     err        = SndPlay ( chan,  sndHdl,  async );
  41.     
  42.     ReleaseResource ( (Handle)sndHdl );
  43. }
  44.  
  45.  
  46. void
  47. GrabFrontWindowAsJPEG ( unsigned short *width, unsigned short *height )
  48. {
  49.     LayerPtr     rootLayer;
  50.     WindowPtr     frontmostWindow;
  51.     Rect        frontmostRect;
  52.     short        theRefNum;
  53.     short        vRefNum;
  54.     Str255        fileName;
  55.     long        startTime;
  56.     OSErr         err;
  57.     Ptr                        compressedResultPtr;
  58.     PixMapHandle            frontmostPixMap;
  59.     ImageDescriptionHandle    imageDescriptionHandle;
  60.     
  61.     startTime = TickCoun t();
  62.     
  63.     /* 4000000 is a totally arbitrary 'very big' value */
  64.     compressedResultPtr = NewPtr ( 4000000 );
  65.     err = MemError ();
  66.     if ( err != noErr )
  67.     {
  68.         *width = nil;
  69.         *height = nil;
  70.         return;
  71.     }
  72.     
  73.     imageDescriptionHandle    = (ImageDescriptionHandle) NewHandle ( sizeof(ImageDescription) );
  74.     err = MemError ();
  75.     if ( err != noErr )
  76.     {
  77.         *width    = nil;
  78.         *height    = nil;
  79.         return;
  80.     }
  81.     
  82. // Find the front layer
  83.     rootLayer = GetRootLayer ();
  84.     
  85. // get the first non-floating window
  86.     frontmostWindow = GetSelectable ( rootLayer );
  87.     
  88. //save the port    
  89. //    GetPort(&thePort);
  90.  
  91. //    SetPort(frontmostWindow);
  92.     frontmostRect = frontmostWindow->portRect;
  93.  
  94. //remember the dimensions so we can tell the caller    
  95.     *width    = frontmostRect.right - frontmostRect.left;
  96.     *height    = frontmostRect.bottom - frontmostRect.top;
  97.     
  98.     /* "What's the point of doing a hack here without all the help?" */
  99.  
  100.     /* "Do you want me to show you how that works?"
  101.         -- Eric Traut, to Sean Parent */
  102.  
  103. //    GetCWMgrPort(&wMgrCPort);
  104. //    SetPort((GrafPtr)wMgrCPort);
  105.  
  106. //Grab the bits out of the frontmost window and compress them with the jpeg compressor
  107.     frontmostPixMap = ((CGrafPtr)frontmostWindow)->portPixMap;
  108.     
  109.     err = CompressImage ( frontmostPixMap, &frontmostRect, codecNormalQuality, 'jpeg', imageDescriptionHandle, compressedResultPtr );
  110.     if ( err != noErr )
  111.     {
  112.         *width    = nil;
  113.         *height    = nil;
  114.         return;
  115.     }
  116.  
  117. //    SetPort(thePort);
  118.  
  119.     /* "they're running out of ideas, for sure"
  120.         -- Grant Neufeld, on Hollywood, for opening logo sequences */
  121.  
  122. //Create a file and stuff it full of the jpeg data
  123.  
  124.     GetVol    ( fileName, &vRefNum );
  125.     strcpy    ( (char*)fileName, (char*)"\pfrontwindow.jpg" );
  126.     
  127.     err = FSDelete ( fileName, vRefNum );
  128.     if ( err != noErr )
  129.     {
  130.         *width    = nil;
  131.         *height    = nil;
  132.         return;
  133.     }
  134.     
  135.     err = Create ( fileName, vRefNum, 'JVWR', 'JPEG' );
  136.     if ( err != noErr )
  137.     {
  138.         *width    = nil;
  139.         *height    = nil;
  140.         return;
  141.     }
  142.     
  143.     err = FSOpen ( fileName, vRefNum, &theRefNum );
  144.     if ( err != noErr )
  145.     {
  146.         *width    = nil;
  147.         *height    = nil;
  148.         return;
  149.     }
  150.     
  151.     {
  152.         long    count;
  153.         
  154.         count    = (*imageDescriptionHandle)->dataSize;
  155.         err        = FSWrite ( theRefNum, &count, compressedResultPtr );
  156.         if ( err != noErr )
  157.         {
  158.             *width    = nil;
  159.             *height    = nil;
  160.             return;
  161.         }
  162.     }
  163.     err = FSClose ( theRefNum );
  164.     if ( err != noErr )
  165.     {
  166.         *width    = nil;
  167.         *height    = nil;
  168.         return;
  169.     }
  170.     
  171. //clean up
  172.     DisposeHandle    ( (Handle)imageDescriptionHandle );
  173.     DisposePtr        ( compressedResultPtr );
  174. }
  175.  
  176.  
  177. /* this function doesn't really work :-(
  178.     It's supposed to move the mouse to the coordinates h & v (local) in the frontmostwindow's
  179.     port (converting to global coordinates) and post a click event at that location */
  180. void
  181. ClickOnTheFrontWindow ( unsigned short h, unsigned short v )
  182. {
  183.     long        eventMsg;
  184.     short        err;
  185.     Point        thePt;
  186.     LayerPtr     rootLayer;
  187.     WindowPtr     frontmostWindow;
  188.     GrafPtr     thePort;
  189.     EvQElPtr    eventPtr;    
  190.     
  191. // Find the front layer
  192.     rootLayer = GetRootLayer();
  193. // get the first non-floating window
  194.     frontmostWindow = GetSelectable(rootLayer);
  195.     
  196. //save the port    
  197.     GetPort ( &thePort );
  198.     SetPort ( frontmostWindow );
  199.  
  200.     thePt.h = h;
  201.     thePt.v = v;
  202.     
  203.     LocalToGlobal ( &thePt );
  204.     
  205.     eventMsg = (((long)thePt.v)<<16) + thePt.h;
  206.  
  207.     err = PPostEvent ( mouseDown, eventMsg, &eventPtr );
  208.     
  209.     eventPtr->evtQWhere = thePt;
  210.  
  211. //restore the port
  212.     SetPort ( thePort );
  213. }
  214.